<?php
//同步obj_last_relate_time 更新到store.wiki.obj_last_relate_time

use Biz\model\wiki\Wiki as mWiki;
use Doraemon\tools\Log;


ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
ini_set('memory_limit', '1024M');
require_once '/data/wwwroot/g_vendor/autoload.php';

$basicIdMax = mWiki::get('id', ['LIMIT' => 1, 'ORDER' => ['id' => 'DESC']]);
$pageSize = 5000;

$page = ceil($basicIdMax / $pageSize);
$i = 0;
try {
    while ($i < $page) {
        $start = $i * $pageSize;
        $end = ($i + 1) * $pageSize;
        $data = mWiki::select('id', ['id[<>]' => [$start, $end], 'obj_last_relate_time' => 0]);
        echo '这是第'.($i + 1).'批数据处理开始,共：'.(count($data)).'条数据'.PHP_EOL;
        Log::info('这是第'.($i + 1).'批数据处理开始,共：'.(count($data)).'条数据');
        if ($data) {
            foreach ($data as $wikiId) {
                $obj_last_relate_time = \Biz\service\wiki\ObjRelateWiki::getLastWikiRelateTime($wikiId) ?? 0;
                mWiki::update(['obj_last_relate_time' => $obj_last_relate_time], ['id' => $wikiId]);
            }
        }
        echo '这是第'.($i + 1).'批数据处理结束,共：'.(count($data)).'条数据被处理'.PHP_EOL;
        Log::info('这是第'.($i + 1).'批数据处理结束,共：'.(count($data)).'条数据被处理');
        $i++;
    }
} catch (Exception $exception) {
    Log::info('同步obj_last_relate_time', ['exception' => $exception]);
}